package com.education.wisdom.controller.app;


import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateLiuUtil;
import com.education.common.utils.StringUtils;
import com.education.wisdom.pojo.Catalog;
import com.education.wisdom.pojo.CurrencyRecord;
import com.education.wisdom.pojo.JrOrder;
import com.education.wisdom.pojo.LessonResources;
import com.education.wisdom.service.ICatalogService;
import com.education.wisdom.service.ICurrencyRecordService;
import com.education.wisdom.service.IJrOrderService;
import com.education.wisdom.service.LessonResourcesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单相关操作
 *
 * @author ZJZ
 */
@RestController
@RequestMapping("/order")
public class AppOrderController {

    @Autowired
    private IJrOrderService orderService;
    @Autowired
    private ICurrencyRecordService currencyRecordService;
    @Autowired
    private ICatalogService catalogService;
    @Autowired
    private LessonResourcesService lessonResourcesService;


    /**
     * 订单号生成   20200718112629527
     */
    public static String nextItemNo() {
        LocalDateTime dateTime = LocalDateTime.now();
        return DateLiuUtil.getOtherFormatterLocalDateTime(dateTime);
    }

    /**
     * 格式化时间
     */
    public void formatTime(JrOrder jrOrder) {
        //格式化创建日期
        LocalDateTime ldt = jrOrder.getCreateTime().toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        String dateTime = DateLiuUtil.getAllFormatterLocalDateTime(ldt);
        jrOrder.setCreateTimeString(dateTime);
        if (jrOrder.getPaymentTime() != null) {
            //格式化付款日期
            LocalDateTime ldt1 = jrOrder.getPaymentTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            String dateTime1 = DateLiuUtil.getAllFormatterLocalDateTime(ldt1);
            jrOrder.setPaymentTimeString(dateTime1);
        }
    }


    /**
     * 列表查询 分类
     */
    @GetMapping("/list")
    public ApiReturnObject list(Integer pageNum, Integer pageSize, JrOrder order) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<JrOrder> jrOrders = orderService.selectJrOrderList(order);
        for (JrOrder jrOrder : jrOrders) {
            formatTime(jrOrder);
        }
        PageInfo<JrOrder> pageInfo = new PageInfo<>(jrOrders);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }


    /**
     * 安卓查找我的订单列表-查询购课消费记录
     * 苹果查找我的订单列表-查询购课消费记录(全部  已完成status1  已关闭status0)
     *
     * @param pageNum
     * @param pageSize
     * @param currencyRecord
     * @return
     */
    @GetMapping("/listOther")
    public ApiReturnObject listOther(Integer pageNum, Integer pageSize, CurrencyRecord currencyRecord) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        //查询消费记录
        currencyRecord.setTrxType("3");
        List<CurrencyRecord> currencyRecords = currencyRecordService.selectCurrencyRecordListOtherTwo(currencyRecord);
        ArrayList<JrOrder> jrOrders = Lists.newArrayList();
        for (CurrencyRecord record : currencyRecords) {
            JrOrder jrOrder = new JrOrder();
            jrOrder.setId(record.getId());
            //订单号
            jrOrder.setOrderId(record.getCurrencyId());
            //创建时间
            jrOrder.setCreateTime(record.getCreateTime());
            //支付时间
            jrOrder.setPaymentTime(record.getEditTime());
            //支付状态
            jrOrder.setStatus(record.getStatus());
            //数量
            jrOrder.setOrderNum(1);
            if (StringUtils.isNotEmpty(record.getOrderType()) && "2".equals(record.getOrderType())) {
                //根据共享备课资源查询字段
                LessonResources lessonResources = lessonResourcesService.selectLessonResourcesById(record.getCatalogId());
                //订单课程名称
                jrOrder.setName(lessonResources.getRName());
                //订单课程图片
                jrOrder.setCatalogIco(lessonResources.getResIco());
            } else {
                //根据课程查询字段
                Catalog catalog = catalogService.selectCatalogByCatalogId(record.getCatalogId());
                if (StringUtils.isNotNull(catalog)) {
                    //订单课程名称
                    jrOrder.setName(catalog.getName());
                    //订单课程图片
                    jrOrder.setCatalogIco(catalog.getCatalogIco());
                } else {
                    //购买的课程已经被删除 赋默认值
                    //订单课程名称
                    jrOrder.setName("");
                    //订单课程图片
                    jrOrder.setCatalogIco("1597476296318.jpg");
                }
            }
            //课程单价 实付金额
            jrOrder.setActualPay(record.getCurrency().toString());
            jrOrders.add(jrOrder);
        }
        for (JrOrder jrOrder : jrOrders) {
            formatTime(jrOrder);
        }
        PageInfo<JrOrder> pageInfo = new PageInfo<>(jrOrders);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }

    /**
     * 订单详情 状态 课程名称 购买数量 订单编号 创建时间 付款时间 有效日期 课程单价 优惠金额 实付金额
     *
     * @param id
     * @return
     */
    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id) {
        if (id != null) {
            CurrencyRecord currencyRecord = currencyRecordService.selectCurrencyRecordById(id);
            JrOrder jrOrder = new JrOrder();
            //订单编号
            jrOrder.setOrderId(currencyRecord.getCurrencyId());
            jrOrder.setStatus(currencyRecord.getStatus());
            //创建时间
            jrOrder.setCreateTime(currencyRecord.getCreateTime());
            //付款时间
            jrOrder.setPaymentTime(currencyRecord.getEditTime());
            //有效日期
            jrOrder.setEffectiveDate("长期有效");
            //购买数量
            jrOrder.setOrderNum(1);
            //课程单价 实付金额
            jrOrder.setActualPay(currencyRecord.getCurrency().toString());
            //优惠金额
            jrOrder.setCoupon("0");
            if (StringUtils.isNotEmpty(currencyRecord.getOrderType()) && "2".equals(currencyRecord.getOrderType())) {
                //根据共享备课资源查询字段
                LessonResources lessonResources = lessonResourcesService.selectLessonResourcesById(currencyRecord.getCatalogId());
                //订单课程名称
                jrOrder.setName(lessonResources.getRName());
                //订单课程图片
                jrOrder.setCatalogIco(lessonResources.getResIco());
                jrOrder.setPrice(lessonResources.getPrice());
            } else {
                //根据课程查询字段
                Catalog catalog = catalogService.selectCatalogByCatalogId(currencyRecord.getCatalogId());
                if (StringUtils.isNotNull(catalog)) {
                    //订单课程名称
                    jrOrder.setName(catalog.getName());
                    //订单课程图片
                    jrOrder.setCatalogIco(catalog.getCatalogIco());
                    jrOrder.setPrice(catalog.getPrice());
                } else {
                    //购买的课程已经被删除 赋默认值
                    //订单课程名称
                    jrOrder.setName("");
                    //订单课程图片
                    jrOrder.setCatalogIco("1597476296318.jpg");
                }
            }

            formatTime(jrOrder);
            return new ApiReturnObject("200", "查询成功", jrOrder);
        }
        return new ApiReturnObject("201", "缺少参数id", "");
    }

    /**
     * 添加
     * 购买课程相关信息
     * 具体支付达人币相关信息
     * 生成订单相关信息
     * 参数： userId  catalogId
     */
    @PostMapping("/add")
    public ApiReturnObject createOrder() {
        //生成订单号
        String orderNum = nextItemNo();
        //根据订单id查询相关信息
        //根据userId查询相关信息
        return new ApiReturnObject("200", "购买成功", "");
    }


    /**
     * 删除
     */
    @PostMapping("/del")
    public ApiReturnObject del(JrOrder order) {
        if (null != order.getId()) {
            orderService.deleteJrOrderByIds(order.getId().toString());
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少参数id", "");
    }

}


